package com.example.bilibili.home;

import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.example.bilibili.Constants;
import com.example.bilibili.R;
import com.example.bilibili.ResultBeanData;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;
import androidx.recyclerview.widget.GridLayoutManager;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.RecyclerView;
import okhttp3.Call;

public class TuijianFragment extends androidx.fragment.app.Fragment {

    private static final String TAG = TuijianFragment.class.getSimpleName();
    private RecyclerView rvHome2;
    private ImageButton ib_top;


    private TuijianFragmentAdapter adapter;
    private ResultBeanData.ResultBean resultBean;

    protected Context mContext;
    //当该类被系统创建的时候被回调
    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mContext=getActivity();
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return initview();
    }

    private View initview() {
        Log.e(TAG, "主页视图被初始化了");
        View view = View.inflate(mContext, R.layout.fragment_tuijian, null);
        rvHome2 = (RecyclerView) view.findViewById(R.id.rv_home2);
        ib_top = (ImageButton) view.findViewById(R.id.ib_top);

        //设置点击事件
        initListener();
        return view;
    }

    private void initListener() {
        //置顶按钮的监听
        ib_top.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //回到顶部
                rvHome2.scrollToPosition(0);
            }
        });


    }

    //当Activity被创建的时候回调这个方法
    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        initData();
    }

    private void initData() {
        Log.e(TAG, "主页数据被初始化了");
        //联网请求主页的数据
        getDataFromNet();
    }

    private void getDataFromNet() {
        String url = Constants.HOME_URL;
        OkHttpUtils
                .get()
                .url(url)
                .build()
                .execute(new StringCallback() {

                    @Override
                    public void onError(Call call, Exception e, int id) {

                        Log.e(TAG,"首页请求失败=="+e.getMessage());
                    }

                    @Override
                    public void onResponse(String response, int id) {
                        Log.e(TAG,"首页请求成功==");
                        //解析数据
                        processData(response);
                    }


                });
    }

    private void processData(String json) {
        ResultBeanData resultBeanData = JSON.parseObject(json,ResultBeanData.class);
        resultBean = resultBeanData.getResult();
        if(resultBean != null){
            //有数据
            //设置适配器
            adapter = new TuijianFragmentAdapter(mContext,resultBean);
            rvHome2.setAdapter(adapter);
            GridLayoutManager manager =  new GridLayoutManager(mContext,1);
            //设置布局管理者
            rvHome2.setLayoutManager(manager);

        }else{
            //没有数据
        }
    }

}
